package net.lwf.jaxb.entity;

import net.lwf.common.constant.ICommonConstant;


public class Fraction implements Comparable{
	
	
	private int numerator;
	private int denominator;
	
	//For mixed
	private boolean isMixed = false;
	private int remainder = 0;
	private int quotient = 0;
	
	public Fraction(int numerator, int denominator) {
		super();
		this.numerator = numerator;
		this.denominator = denominator;
	}

	public int getNumerator() {
		return numerator;
	}

	public int getDenominator() {
		return denominator;
	}
	
	public double getDecimalFraction() {
		return (double) this.numerator/this.denominator;
	}
	
	public boolean isMixed() {
		if(numerator > denominator) {
			quotient = numerator/denominator;
			remainder = numerator % denominator;
			if(remainder == 0) {
				return isMixed;
			}
			else {
				isMixed = true;
			}
			return true;
		}
		return isMixed;
	}
	
	public boolean isZero() {
		if(this.numerator == 0 && this.denominator == 0) {
			return true;
		}
		return false;
	}
	
	public String toString() {

		if(isMixed()) {
			return quotient + ICommonConstant.ONE_SPACE + this.remainder + ICommonConstant.FORWARD_SLASH + this.denominator;
		}
		if(this.numerator == 0) {
			return "0";
		}
		if(this.numerator/this.denominator == 1) {
			return "1";
		}
		return this.numerator + ICommonConstant.FORWARD_SLASH + this.denominator;
	}
	
	public String toFraction() {
		if(this.numerator/this.denominator == 1) {
			return "1";
		}
		return this.numerator + ICommonConstant.FORWARD_SLASH + this.denominator;
	}
	
	public Double compareKey() {
		return new Double(getDecimalFraction());
	}
	public int compareTo(Object o) {
        Fraction frac = (Fraction) o;
        int i = compareKey().compareTo(frac.compareKey());
		return i;
	}

	public String toHtml() {
  		StringBuffer sbHtml = new StringBuffer("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"red\" frame=\"box\" rules=\"none\" >");
		sbHtml.append("<tr>");
			sbHtml.append("<td class=\"fractiondisplay\"> " + this.numerator + " </td>");
		sbHtml.append("</tr>");
		sbHtml.append("<tr>");
			sbHtml.append("<td> <hr style=\"border: none solid #000;\" </td>");
		sbHtml.append("</tr>");
		sbHtml.append("<tr>");	
			sbHtml.append("<td class=\"fractiondisplay\"> " + this.denominator + " </td>");
		sbHtml.append("</tr>");
		sbHtml.append("</table>");
		return sbHtml.toString();
	}
	
	public boolean isNagative() {
		if(this.numerator < 0 || this.denominator < 0) {
			return true;
		}
		return false;
	}
	

}
